package wang.xinqiu.mysqlcontrol.data

data class QueryPerformance(
    val query: String,
    val executionTime: Long,
    val rowCount: Int = 0,
    val avgTime: Long = 0L,
    val timestamp: Long = System.currentTimeMillis()
)

class PerformanceMonitor {
    private val queryHistory = mutableListOf<QueryPerformance>()
    
    fun recordQuery(query: String, executionTime: Long, rowCount: Int) {
        queryHistory.add(QueryPerformance(query, executionTime, rowCount))
        if (queryHistory.size > 100) {
            queryHistory.removeAt(0)
        }
    }
    
    fun getAverageExecutionTime(): Long {
        return if (queryHistory.isNotEmpty()) {
            queryHistory.map { it.executionTime }.average().toLong()
        } else 0L
    }
    
    fun getSlowQueries(threshold: Long = 1000): List<QueryPerformance> {
        return queryHistory.filter { it.executionTime > threshold }
            .map { it.copy(avgTime = it.executionTime) }
    }
    
    fun getStats(): String {
        return "总查询数: ${queryHistory.size}, 平均执行时间: ${getAverageExecutionTime()}ms"
    }
    
    fun clearMetrics() {
        queryHistory.clear()
    }
}
